<?php
namespace app\index\service;

use app\index\model\AcckuanModel;
use app\index\model\AccountListModel;
use app\index\model\DealbianListModel;
use app\index\model\DealbianModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;

class FinanceService extends CommonService
{

    public function check_dealcom_name($name, $id=0){
        return !!DealcomModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0,'isturn'=>1])
            ->where('status','in', [0,1])->where('id','neq', $id)->find();
    }
    public function set_dealcom_zhanginfo($id){
        $banknum = '';
        $bankname = '';
        $deainfo = DealcomListModel::where(['dealid'=>$id,'isdea'=>1])->find();
        if($deainfo){
            $banknum = $deainfo['zhangname'].$deainfo['banknum'];
            $bankname = $deainfo['bankname'];
        }
        return DealcomModel::where(['id'=>$id])->update(['banknum'=>$banknum,'bankname'=>$bankname]);
    }
    public function set_dealbian_zhanginfo($id){
        $banknum = '';
        $bankname = '';
        $deainfo = DealbianListModel::where(['dealid'=>$id,'isdea'=>1])->find();
        if($deainfo){
            $banknum = $deainfo['zhangname'].$deainfo['banknum'];
            $bankname = $deainfo['bankname'];
        }
        return DealbianModel::where(['id'=>$id])->update(['banknum'=>$banknum,'bankname'=>$bankname]);
    }
    public function create_account_list_jiaonum_zhi(){
        $tou = 'ZC';
        $daystr = substr(date("Ymd"), 2);
        $number = AccountListModel::where('jiaonum','like','%'.$tou.$daystr.'%')->order('jiaonum DESC')->value('jiaonum');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }
    public function create_account_list_jiaonum_shou(){
        $tou = 'SR';
        $daystr = substr(date("Ymd"), 2);
        $number = AccountListModel::where('jiaonum','like','%'.$tou.$daystr.'%')->order('jiaonum DESC')->value('jiaonum');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }
    public function get_payfee_by_mid($modtype, $mid){
        $payfee = AcckuanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['modtype'=>$modtype,'mid'=>$mid])->value('payfee');
        return $payfee;
    }

    public function get_acckuan_info_bybillid($billid){
        $kuaninfo = AcckuanModel::where(['billid'=>$billid])->find();
        if($kuaninfo){
            $kuaninfo['paystate_name'] = (new KuanService())->get_acckuan_paystate_name($kuaninfo['paystate']);
        }
        return $kuaninfo;
    }

}

